/*
 Main program-- command-line argument handling and top-level control.
*/
#include <stdlib.h>
#include <string.h>
#include "usb_pickit.h"

/* Write .hex file data into a chip */
const char *programMode(usb_pickit *d,const char *inFile,int keepOld) {
	FILE *f=fopen(inFile,"r");
	pic14_state p;
	const char *err;
	if (f==NULL) return "could not find program file";
	err=pic14_hex_read(&p,f);
	if (err!=NULL) return err;
	fclose(f);
	usb_pickit_write(d,&p,keepOld);
	return NULL;
}

/* Read chip data into a .hex file */
const char *extractMode(usb_pickit *d,const char *outFile) {
	FILE *f=fopen(outFile,"w");
	pic14_state p;
	if (f==NULL) return "could not create output file";
	usb_pickit_read(d,&p);
	pic14_hex_write(&p,f);
	fclose(f);
	return NULL;
}

void printUsage(const char *why) {
	printf(
		" Microchip(tm) PICkit(tm) 1 USB Programmer controller program\n"
		"\n"
		"Usage: one of\n"
		" usb_pickit <file>  (Writes .hex file to chip)\n"
		" usb_pickit --program <file> (Verbose version of above)\n"
		" usb_pickit --extract <file> (Read from chip into .hex file)\n"
		" usb_pickit --config (Show configuration data)\n"
		" usb_pickit --reset (Power cycle the chip)\n"
		" usb_pickit --off   (Turn chip power off)\n"
		" usb_pickit --on    (Turn chip power back on)\n"
		" usb_pickit --programall <file> (Overwrite osccal and bg (!))\n"
		" usb_pickit --help (This message)\n"
		"   <file> is an Intel MDS .hex file\n"
		" Version 1.21, Orion Sky Lawlor, olawlor@acm.org, 2004/1/19\n"
		" Exiting: %s\n",why); 
	exit(1);
}
#ifndef COCOA_APP
int main(int argc,char *argv[]) {
#else
int doPicCommand( int argc, char *argv[]) {
#endif
	const char *err=NULL;
	const char *arg;
	if (argc<2) printUsage("no mode argument given");
	arg=argv[1];
	if (0==strcmp(arg,"--program")) {
		if (argc!=3) printUsage("program mode argument error");
		usb_pickit *d=usb_pickit_open();
		err=programMode(d,argv[2],1);
	} else if (0==strcmp(arg,"--programall")) {
		if (argc!=3) printUsage("program mode argument error");
		usb_pickit *d=usb_pickit_open();
		err=programMode(d,argv[2],0);
	} else if (0==strcmp(arg,"--extract")) {
		if (argc!=3) printUsage("extract mode argument error");
		usb_pickit *d=usb_pickit_open();
		err=extractMode(d,argv[2]);
	} else if (0==strcmp(arg,"--config")) {
		usb_pickit *d=usb_pickit_open();
		usb_pickit_print_config(d);
	} else if (0==strcmp(arg,"--off")) {
		usb_pickit *d=usb_pickit_open();
		usb_pickit_off(d);
	} else if (0==strcmp(arg,"--on")) {
		usb_pickit *d=usb_pickit_open();
		usb_pickit_on(d);
	} else if (0==strcmp(arg,"--reset")) {
		usb_pickit *d=usb_pickit_open();
		usb_pickit_off(d);
		usb_pickit_on(d);
	} else if (0==strcmp(arg,"--help") || 0==strcmp(arg,"-h")) {
		printUsage("command line usage");
	} 
	else if (arg[0]=='-') {
		printUsage("unrecognized mode argument");
	}
	else /* non-flag argument-- treat as file */ {
		usb_pickit *d=usb_pickit_open();
		err=programMode(d,arg,1);
	}
	
#ifndef COCOA_APP
	if (err) {fprintf(stderr,"Fatal error> %s\n",err);return 0;}
	return 0;
#else
	return err;
#endif
}
